package com.informationsystem.model;

import com.informationsystem.model.BaseObject;

import org.apache.commons.lang.StringEscapeUtils;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

import javax.xml.bind.annotation.XmlRootElement;

import java.io.Serializable;

@Entity
@Table(name="event")
@Indexed
@XmlRootElement
public class Event extends Information implements Serializable {
    private Date startDate = new Date();
    private Date endDate = new Date();
    private String type;
    private String location;
    private String content;
    
    @Temporal(TemporalType.DATE)
    @Column(name="start_date", nullable=false, length=10)
    @Field
    public Date getStartDate() {
        return this.startDate;
    }
    
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    @Temporal(TemporalType.DATE)
    @Column(name="end_date", nullable=false, length=10)
    @Field
    public Date getEndDate() {
        return this.endDate;
    }
    
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
    
    @Column(name="type", nullable=false, length=20)
    @Field
    public String getType() {
        return this.type;
    }
    
    public void setType(String type) {
        this.type = type;
    }
    
    @Column(name="location", nullable=false, length=50)
    @Field
    public String getLocation() {
        return this.location;
    }
    
    public void setLocation(String location) {
        this.location = location;
    }
    
    @Column(name="content", nullable=false, length=10000)
    @Field
    public String getContent() {
        return this.content;
    }
    
    public void setContent(String content) {
        this.content = content;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Event pojo = (Event) o;

        if (startDate != null ? !startDate.equals(pojo.startDate) : pojo.startDate != null) return false;
        if (endDate != null ? !endDate.equals(pojo.endDate) : pojo.endDate != null) return false;
        if (type != null ? !type.equals(pojo.type) : pojo.type != null) return false;
        if (location != null ? !location.equals(pojo.location) : pojo.location != null) return false;
        if (content != null ? !content.equals(pojo.content) : pojo.content != null) return false;

        return true;
    }

    public int hashCode() {
        int result = 0;
        result = 31 * result + (startDate != null ? startDate.hashCode() : 0);
        result = 31 * result + (endDate != null ? endDate.hashCode() : 0);
        result = 31 * result + (type != null ? type.hashCode() : 0);
        result = 31 * result + (location != null ? location.hashCode() : 0);
        result = 31 * result + (content != null ? content.hashCode() : 0);

        return result;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer(getClass().getSimpleName());

        sb.append(" [");
        sb.append("id").append("='").append(getId()).append("', ");
        sb.append("startDate").append("='").append(getStartDate()).append("', ");
        sb.append("endDate").append("='").append(getEndDate()).append("', ");
        sb.append("type").append("='").append(getType()).append("', ");
        sb.append("location").append("='").append(getLocation()).append("', ");
        sb.append("content").append("='").append(getContent()).append("'");
        sb.append("]");
      
        return sb.toString();
    }
    
	@Transient
	public String getShortContent(){
		return stripHtml(getDisplayContent(),200);
	}
	
	@Transient
	public String stripHtml(String input, int length){
        if (input == null || input.trim().equals("")) {  
            return "";  
        }  
        String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(  
                "<[^>]*>", "");  
        str = str.replaceAll("[(/>)<]", "");  
        int len = str.length();  
        if (len <= length) {  
            return str;  
        } else {  
            str = str.substring(0, length);  
            str += "......";  
        }  
        return str;  
	}
	@Transient
	public String getDisplayContent(){
		return StringEscapeUtils.unescapeHtml(content);
	}
	
	@Transient
	public String getDisplayStartDate(){
		SimpleDateFormat dt = new SimpleDateFormat("MM-dd-yyyy"); 
		return dt.format(endDate);
	}   
	
	@Transient
	public String getDisplayEndDate(){
		SimpleDateFormat dt = new SimpleDateFormat("MM-dd-yyyy"); 
		return dt.format(startDate);
	}   
}
